package klimuts.periodicals.user.actions;

import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import klimuts.periodicals.data.DataSource;
import klimuts.periodicals.user.User;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.validator.annotations.EmailValidator;

/**
 * Action class used for registration new users at registration.jsp.
 * After success validation users data has added to the
 * data base and new user have been logged on.
 * @author Siarhei Klimuts 
 */
public class Registration extends ActionSupport implements SessionAware {
	private Map<String,Object> session;
	private String firstName;
    private String lastName;
    private String email;
    private String login; 
    private String password; 
    private String confirm;
    private String address;

	public String execute() throws Exception {
		User user=DataSource.addUser(new User((byte)0, firstName, lastName, email, login, password, address));
		if (user!=null){
			session.put("user", user); 
			addActionMessage(getText("user.registration.success"));
			return "success";
		} else {
			addActionError(getText("user.registration.error"));
			return "input";
		}
	}
	
	public void validate(){
		if (isOutOfRange(firstName, 3, 20))
			addFieldError("firstName",getText("user.firstName.incorrect"));
		if (isOutOfRange(lastName, 3, 20))
			addFieldError("lastName",getText("user.lastName.incorrect"));
		if (isEmailIncorrect())
			addFieldError("email",getText("user.email.incorrect"));
		if (isOutOfRange(login, 3, 20))
			addFieldError("login",getText("user.login.incorrect"));
		else if (DataSource.getUser(login))
			addFieldError("login",getText("user.login.exists"));
		if (isOutOfRange(password, 3, 20))
			addFieldError("password",getText("user.password.incorrect"));
		if (!confirm.equals(password))
			addFieldError("confirm",getText("user.confirmPassword.incorrect"));
		if (isOutOfRange(address, 10, 100))
			addFieldError("address",getText("user.address.incorrect"));
	}

	/**
	 * 
	 * @param str - {@link String} 
	 * @param l1 - minimal length
	 * @param l2 - maximal length
	 * @return <code>true</code> if str is out of range or <code>false</code> if not
	 */
	private boolean isOutOfRange(String str, int l1, int l2){
		return ((str.length()<l1)||(str.length()>l2));
	}
	
	/**
	 * Simple email validation, email like x@x.x is valid
	 * @return Return <code>true</code> if email is not correct, <code>false</code> if correct.
	 */
	private boolean isEmailIncorrect(){
		// TODO find struts validation
		int symbol=email.indexOf("@");
		int dot=email.indexOf(".");
		int length=email.length()-1;
		return !((symbol>0)&&(dot>symbol+1)&&(dot<length));
	}

	public void setSession(Map<String,Object> session) {
		this.session=session;
	}
    
	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getConfirm() {
		return confirm;
	}

	public void setConfirm(String confirm) {
		this.confirm = confirm;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}
}
